/* 
 * This file is part of FreeBMS - an open source battery management system
 *
 * Serial Headers
 * 
 * NOTE: This code is designed to be compiled using SOURCEBOOST (http://www.sourceboost.com)
 */
 
/* This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
/*************************************************************
* Blocking serial interface code for the hardware UART
* of PIC16-range microcontrollers from Microchip
*
* (c) Lieven Hollevoet
* most recent version on http://boostc.lika.be
* Based on code from Yann Hendrikx.
*************************************************************/

#ifndef _SERIAL_H_
#define _SERIAL_H_

#include <system.h>
#include "types.h"

// Function declarations
void serial_init(char brg);
void serial_print_hex(char number);
void serial_printf(char value);
char serial_getch();

// Macros for simple functions (to reduce stack usage)
#define serial_peek()       (pir1 & 1 << RCIF)
#define serial_print_lf() 	serial_printf(0x0a); serial_printf(0x0d)

// FREEBMS ADDITIONS
#define MAX_MESG_SIZE   16	// Number of bytes possible in a message
#define MAX_DATA_SIZE	8	// Number of data-bytes in a message

extern BYTE rxbuffer[MAX_MESG_SIZE];	// Buffer for sending messages
extern BYTE rxptr;						// Pointer for receiving messages
extern BYTE rxlength;					// Length of message being received
extern BYTE rxescape;					// Whether previous character was escape character
extern BYTE mesg_data[MAX_DATA_SIZE];	// Message argument buffer
extern BYTE chip_id;					// ID of current chip

// Function declarations
BYTE send_message(short mesg_type, short dest_from, BYTE length);
BYTE receive_message();
#if DEBUG==1
void rxbuffer_to_vars();
void txbuffer_to_vars(char *buffer);
#endif

#endif // _SERIAL_H_